// *** ADDED BY HEADER FIXUP ***
#include <wx/app.h>
// *** END ***
/***************************************************************
 * Name:      ServerApp.cpp
 * Purpose:   Code for Application Class
 * Author:    sleepiforest (248023406@qq.com)
 * Created:   2011-11-28
 * Copyright: sleepiforest (http://weibo.com/sleepiforest)
 * License:
 **************************************************************/

#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#include "Alice/Kernel.h"
#include <wx/socket.h>
#include "LoginSet.h"
#include "LinkThread.h"
#include "PhoneAimlManager.h"
#include <wx/dir.h>
#include <iostream>
using namespace std;

/*void PrintOx(const void* s)
{
    cout<<(char *)s<<":  ";
    for(int i=0;i<6;++i)
    {
        printf("%02x",*(unsigned char*)(s+i));
    }
    cout<<endl;
}

void TestDir()
{
    wxString dirp = wxGetCwd()+wxT("/PhoneAiml");
    wxDir dir(dirp);
    if(!dir.IsOpened()) puts("ERROR");
    printf("dir: %s\n",dirp.mb_str(wxConvUTF8).data());
    wxString fn;
    for(bool t=dir.GetFirst(&fn);t;t=dir.GetNext(&fn))
    {
        FILE* file = fopen("新文件","r");
        if(file==NULL) puts("ERROR");
        PrintOx(fn.mb_str(wxConvISO8859_1).data());
    }
}*/
void Test()
{
    printf("wxchar: %d\n",sizeof(wxChar));
    //system("pause");
    //system("pause");
    getchar();getchar();
}
int main(int argc,char ** argv)
{
	extern bool wxInitialize(int,char **);
    wxInitialize(argc,argv);
    wxSocketBase::Initialize();
    Kernel kernel;
    kernel.bootstrap();
    //Test();
    PhoneAimlManager pam(&kernel);
    int port = 9800;
    if(argc>1) sscanf(argv[1],"%d",&port);
	wxIPV4address addr;
  	addr.Service(port);
  	wxSocketServer *server = new wxSocketServer(addr);
  	if(server->IsOk())
	{
		printf("Server has been listening to port %d ~\n",port);
		LoginSet loger;
		while(true)
		{
			wxSocketBase *sock = server->Accept();
			if(sock) new LinkThread(&kernel,sock,&loger,&pam);
		}
	}
  	else puts("Could not listen at the specified port !\n\n");
	return 0;
}
